/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package per.wsj.andratingbar.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import per.wsj.andratingbar.ResourceTable;
import per.wsj.andratingbar.ui.ListFragment;
import per.wsj.andratingbar.ui.SizeFragment;
import per.wsj.andratingbar.ui.SpacingFragment;
import per.wsj.andratingbar.ui.StyleFragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MainAbilitySlice
 *
 * @since 2021-04-13
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final int ZERO = 0;
    private static final int ONE = 1;
    private static final int TWO = 2;
    private static final int THREE = 3;

    private static final int BUTTON_NUM = 4;
    private List<DirectionalLayout> tabList;
    private Map<Integer, Element> resMap = new HashMap<>();
    private Map<Integer, Element> resMapFocus = new HashMap<>();
    private Map<Integer, Integer> resMapText = new HashMap<>();
    private Text mTitleText;
    private ResourceManager mResManager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mResManager = this.getResourceManager();
        try {
            int color = mResManager.getElement(ResourceTable.Color_colorPrimaryDark).getColor();
            getWindow().setStatusBarColor(color);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.fillInStackTrace();
        }

        mTitleText = (Text) findComponentById(ResourceTable.Id_title_text);

        intResMap();
        initBottomTab();
    }

    private void initBottomTab() {
        DirectionalLayout bottomTab = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_tabMenu);
        tabList = new ArrayList<>();

        for (int count = 0; count < BUTTON_NUM; count++) {
            // Use LayoutScatter to convert xml file into Component instance
            DirectionalLayout tab = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_tab, bottomTab, false);
            Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_image);
            Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            buttonImage.setImageElement(resMap.get(count));
            buttonText.setText(resMapText.get(count));

            int finalCount = count;
            tab.setClickedListener(component -> {
                changeFraction(finalCount);
            });
            tabList.add(tab);
            bottomTab.addComponent(tab);
        }
        tabList.get(0).callOnClick();
    }

    private void changeFraction(int index) {
        setSelectState(index);
        mTitleText.setText(resMapText.get(index));
        Fraction fraction = null;
        switch (index) {
            case ZERO:
                fraction = new SizeFragment(getContext());
                break;
            case ONE:
                fraction = new SpacingFragment(getContext());
                break;
            case TWO:
                fraction = new StyleFragment(getContext());
                break;
            case THREE:
                fraction = new ListFragment(getContext());
                break;
            default:
                break;
        }

        ((FractionAbility) getAbility()).getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_container, fraction)
                .submit();
    }

    private void setSelectState(int index) {
        for (int count = 0; count < BUTTON_NUM; count++) {
            DirectionalLayout tab = tabList.get(count);
            Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_image);
            if (count == index) {
                try {
                    int color = mResManager.getElement(ResourceTable.Color_colorPrimary).getColor();
                    buttonText.setTextColor(new Color(color));
                } catch (IOException | NotExistException | WrongTypeException e) {
                    e.fillInStackTrace();
                }
                buttonImage.setImageElement(resMapFocus.get(count));
            } else {
                buttonText.setTextColor(Color.BLACK);
                buttonImage.setImageElement(resMap.get(count));
            }
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void intResMap() {
        resMap.put(ZERO, ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_home_black_24dp));
        resMap.put(ONE, ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_spacing));
        resMap.put(TWO, ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_dashboard_black_24dp));
        resMap.put(THREE, ElementScatter.getInstance(getContext())
                .parse(ResourceTable.Graphic_ic_notifications_black_24dp));

        resMapFocus.put(ZERO, ElementScatter.getInstance(getContext())
                .parse(ResourceTable.Graphic_ic_home_black_24dp_focus));
        resMapFocus.put(ONE, ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_spacing_focus));
        resMapFocus.put(TWO, ElementScatter.getInstance(getContext())
                .parse(ResourceTable.Graphic_ic_dashboard_black_24dp_focus));
        resMapFocus.put(THREE, ElementScatter.getInstance(getContext())
                .parse(ResourceTable.Graphic_ic_notifications_black_24dp_focus));

        resMapText.put(ZERO, ResourceTable.String_title_size);
        resMapText.put(ONE, ResourceTable.String_title_spacing);
        resMapText.put(TWO, ResourceTable.String_title_style);
        resMapText.put(THREE, ResourceTable.String_title_in_list);
    }
}
